<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
     
    </BasicTable>
    <!-- 表单区域 -->
    <CsMealsProductModal @register="registerModal" @success="handleSuccess"></CsMealsProductModal>
  </div>
</template>

<script lang="ts" name="csmealsproduct-csMealsProduct" setup>
  import {reactive} from 'vue';
  import {BasicTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import CsMealsProductModal from './components/CsMealsProductModal.vue'
  import {columns, searchFormSchema} from './CsMealsProduct.data';
  import {list, deleteOne, getImportUrl,getExportUrl,updateStatus} from './CsMealsProduct.api';
  const queryParam = reactive<any>({});
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { tableContext } = useListPage({
      tableProps:{
           title: '餐费产品单',
           api: list,
           columns,
           canResize:false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
                 ['dataRange', ['beginDate', 'endDate'], 'YYYY-MM-DD'],
              ],
            },
           actionColumn: {
               width: 160,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      },
       exportConfig: {
            name:"餐费产品单",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext

   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
 
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }
   

  /**
    * 修改状态事件
    */
  async function handleUpdateStatus(record,status) {
     await updateStatus({id: record.id,status:status},handleSuccess);
   }

   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
         },
         {
           label: '启用',
           onClick: handleUpdateStatus.bind(null, record,1),
           ifShow:record.status=='2'
         },
         {
           label: '停用',
           onClick: handleUpdateStatus.bind(null, record,2),
           ifShow:record.status=='1'
         }
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
          {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
             placement: 'topLeft',
           }
         }
       ]
   }


</script>

<style scoped>

</style>